package servlet;

import dao.MovieMapper;
import entity.Movie;
import org.apache.ibatis.session.SqlSession;
import utils.MyBatisUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/AddServlet")
public class AddServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        SqlSession sqlSession = MyBatisUtil.createSqlSession();

        int recommend = 1;
        if(req.getParameter("recommend")=="不推荐"){
            recommend = 0;
        }

        req.getSession().setAttribute("msg", sqlSession.getMapper(MovieMapper.class).add(new Movie(
                req.getParameter("movieName"),
                req.getParameter("movieType"),
                Double.parseDouble(req.getParameter("price")),
                recommend,
                req.getParameter("startTime"),
                req.getParameter("endTime"),
                Integer.parseInt(req.getParameter("votes"))
        )) > 0);
        sqlSession.commit();
        MyBatisUtil.closeSqlSession(sqlSession);
        resp.sendRedirect("ListServlet");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
